/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import Beans.beanTransportista;
import conexiones.cConexion;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jboss.logging.Logger;

/**
 *
 * @author UFG
 */
public class srvTransportista extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           int var_codigo=Integer.parseInt(request.getParameter("txtCodigo"));
           String var_nombre=request.getParameter("txtNombre");
           String var_telefono=request.getParameter("txtTelefono");
           String var_correo=request.getParameter("txtCorreo");
           String var_direccion=request.getParameter("txtDireccion");
           String var_otros=request.getParameter("txtOtros");
           String var_pais=request.getParameter("cmbPais");
           String var_tipo=request.getParameter("optTipo");
           String var_estado=request.getParameter("chkEstado");
           String var_accion=request.getParameter("cmbTipo");
           
           if(var_accion.equals("I"))
           {
             Connection cnn = cConexion.conectar_ds();
             PreparedStatement psta;
               try {
                   psta = cnn.prepareStatement(beanTransportista.insertar);
                   psta.setInt(1, var_codigo);
                   psta.setString(2, var_nombre);
                   psta.setString(3, var_telefono);
                   psta.setString(4, var_direccion);
                   psta.setString(5, var_correo);
                   psta.setString(6, var_pais);
                   psta.setString(7, var_otros);
                   psta.setString(8, var_tipo);
                   psta.setString(9, var_estado);
                   psta.executeUpdate();
               } catch (SQLException ex) {
                   java.util.logging.Logger.getLogger(srvTransportista.class.getName()).log(Level.SEVERE, null, ex);
               }
           }else if(var_accion.equals("M"))
           {
               Connection cnn = cConexion.conectar_ds();
               PreparedStatement psta;
               try {
                   psta = cnn.prepareStatement(beanTransportista.modificar);
                   psta.setString(1, var_nombre);
                   psta.setString(2, var_telefono);
                   psta.setString(3, var_direccion);
                   psta.setString(4, var_correo);
                   psta.setString(5, var_pais);
                   psta.setString(6, var_otros);
                   psta.setString(7, var_tipo);
                   psta.setString(8, var_estado);
                   psta.setInt(9, var_codigo);
                   psta.executeUpdate();
               } catch (SQLException ex) {
                   java.util.logging.Logger.getLogger(srvTransportista.class.getName()).log(Level.SEVERE, null, ex);
               }
           }else
           {
               Connection cnn = cConexion.conectar_ds();
               PreparedStatement psta;
               try {
                   psta = cnn.prepareStatement(beanTransportista.eliminar);
                   psta.setInt(1, var_codigo);
                   psta.executeUpdate();
               } catch (SQLException ex) {
                   java.util.logging.Logger.getLogger(srvTransportista.class.getName()).log(Level.SEVERE, null, ex);
               }
               
           }
           
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
